import { createRouter, createWebHistory } from "vue-router";
import HelloWorldVue from "../views/HelloWorld.vue";
import HelloWorldVue2 from "../views/HelloWorld2.vue";
import Login from "../views/Login.vue";
import NotFound from "@/views/NotFound.vue";
import ShowCode from "@/views/ShowCode.vue";
import {useLoginStore} from "@/sotre/useLoginStore.js";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/root',
      name: 'root',
      redirect: '/docker',
      component: () => import('@/layout/BaseLayout.vue'),
      children: [
        {
          name: "docker",
          path: "/docker",
          meta: {
            title: "工作台",
          },
          component: HelloWorldVue,
        },
        {
          name: "contract",
          path: "/contract",
          meta: {
            title: "合同管理",
          },
          component:()=>import('@/views/contract/list.vue')
        },
        {
          name: "customer",
          path: "/customer",
          meta: {
            title: "客户管理",
          },
          component:()=>import('@/views/customer/list.vue')
        },
        {
          name: "receivable",
          path: "/receivable",
          meta: {
            title: "账款管理",
          },
          component:()=>import('@/views/receivable/list.vue')
        },
        {
          name: "settings",
          path: "/settings",
          meta: {
            title: "设置中心",
          },
          children: [
            {
              name:"pro-type-setting",
              path:"/pro-type-setting",
              meta:{
                title:"业务分类管理"
              },
              component:()=>import('@/views/proTypeSetting/list.vue')
            },
            {
              name:"com-setting",
              path:"/com-setting",
              meta:{
                title:"公司库管理"
              },
              component:()=>import('@/views/company/list.vue')
            }
          ],
        },
      ]
    },
    {
      name: "login",
      path: "/login",
      meta: {
        title: "登陆",
        hideInMenu: true,
        isGuestPage: true
      },
      component: Login,
    },
    {
      name: "show-code",
      path: "/show-code",
      meta: {
        title: "显示CODE",
        hideInMenu: true,
        isGuestPage: true
      },
      component: ShowCode,
    },
    {
      name: "404",
      path: "/:pathMatch(.*)*",
      meta: {
        title: "404",
        hideInMenu: true,
        isGuestPage: true
      },
      component: NotFound,
    },
  ],
});


router.beforeEach((to, from) => {
  document.title = to.meta.title;
  if(useLoginStore().isLogin && to.meta.isGuestPage){
    return {
      name: 'root'
    }
  }

  if ((!useLoginStore().isLogin || useLoginStore().isExpired) && !to.meta.isGuestPage){
    return {name: '404'}
  }

  // return {name:to.name}
});


export default router;
